package gitee.derumin.lifeissohard.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import gitee.derumin.lifeissohard.lishhungersystem.LishHungerManager;
import gitee.derumin.lifeissohard.tools.LishIdentifiers;
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.gui.screen.recipebook.RecipeBookProvider;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;


@Mixin(InventoryScreen.class)
public abstract class InventoryScreenMixin extends AbstractInventoryScreen<PlayerScreenHandler> implements RecipeBookProvider {

    public InventoryScreenMixin(PlayerScreenHandler screenHandler, PlayerInventory playerInventory, Text text) {
        super(screenHandler, playerInventory, text);
    }

    @Inject(method = "drawBackground" , at = @At(value = "INVOKE" , target = "Lnet/minecraft/client/gui/screen/ingame/InventoryScreen;drawEntity(IIIFFLnet/minecraft/entity/LivingEntity;)V"))
    private void drawFatAndMuscle(MatrixStack matrices, float delta, int mouseX, int mouseY, CallbackInfo ci){
        if (this.client != null && this.client.player != null && this.client.player.getHungerManager() instanceof LishHungerManager lishHungerManager) {
            RenderSystem.setShaderTexture(0, LishIdentifiers.Textures.Gui.FAT_AND_MUSCLE);
            int fat = lishHungerManager.getFat();
            int muscle = lishHungerManager.getMuscle();
            if (fat < 5000000){
                this.drawTexture(matrices, this.x+77, this.y+14, 0, 0, 17, 18);
            } else if (fat < 10000000) {
                this.drawTexture(matrices, this.x+77, this.y+14, 17, 0, 17, 18);
            } else if (fat < 16000000) {
                this.drawTexture(matrices, this.x+77, this.y+14, 34, 0, 17, 18);
            } else if (fat < 24000000) {
                this.drawTexture(matrices, this.x+77, this.y+14, 51, 0, 17, 18);
            }else this.drawTexture(matrices, this.x+77, this.y+14, 68, 0, 17, 18);
            if (muscle < 3000000){
                this.drawTexture(matrices, this.x+77, this.y+37, 0, 18, 17, 18);
            } else if (muscle < 6000000) {
                this.drawTexture(matrices, this.x+77, this.y+37, 17, 18, 17, 18);
            } else if (muscle < 11000000) {
                this.drawTexture(matrices, this.x+77, this.y+37, 34, 18, 17, 18);
            } else if (muscle < 14000000) {
                this.drawTexture(matrices, this.x+77, this.y+37, 51, 18, 17, 18);
            }else this.drawTexture(matrices, this.x+77, this.y+37, 68, 18, 17, 18);
            RenderSystem.setShaderTexture(0, BACKGROUND_TEXTURE);
        }
    }
}
